package com.umeox.watch.moto.dataservice.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.dataservice.base.BaseApp;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.dataservice.db.AppDataBase;
import com.umeox.watch.moto.dataservice.db.model.StoryPlayHistory;
import com.umeox.watch.moto.dataservice.services.ApiService;
import com.umeox.watch.moto.dataservice.services.LocationService;
import com.umeox.watch.moto.dataservice.utils.PushReceiverHelper;

import java.util.List;


/**
 * 自定义接收器
 * <p/>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class BasePushReceiver extends BroadcastReceiver {
    private static final String TAG = BasePushReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Logger.i(TAG, "收到广播： " + intent.getAction());
        if (Constants.ACTION_RESET.equals(intent.getAction())) {
            //解绑
            PushReceiverHelper.getInstance().unbind();

        } else if (Constants.ACTION_ACTIVATING.equals(intent.getAction())) {
            //获取设备初始配置信息
            PushReceiverHelper.getInstance().bind();
        } else if (Constants.ACTION_REFRESH_BUDDY.equals(intent.getAction())) {
            //获取好友列表
            ApiService.getBuddies();
        } else if (Constants.ACTION_REFRESH_CONTACT.equals(intent.getAction())) {
            //获取联系人列表
            ApiService.getContacts();
        } else if (Constants.ACTION_UPLOAD_SOS.equals(intent.getAction())) {
            //调用SOS上传接口通知
            long sosId = intent.getLongExtra(Constants.EXTRA_SOS_ID, 0);
            String fileId = intent.getStringExtra(Constants.EXTRA_FILE_ID);
            ApiService.uploadSos(BaseApp.getInstance().deviceId, sosId, fileId, System.currentTimeMillis());
        } else if (Constants.ACTION_GET_PETINFO.equals(intent.getAction())) {
            ApiService.getStepInfo(BaseApp.getInstance().deviceId);
        } else if (Constants.ACTION_LOCATION.equals(intent.getAction())) {
            int locReason = intent.getIntExtra(Constants.EXTRA_LOC_REASON, -1);
            if (locReason == 0) {
                //服务端请求定位
                String reqUser = intent.getStringExtra(Constants.EXTRA_LOC_USER);
                String locCode = intent.getStringExtra(Constants.EXTRA_LOC_CODE);
                int priority = intent.getIntExtra(Constants.EXTRA_LOC_PRIORITY,0);
                String reportReasonAttr = reqUser + "#" + locCode;
                Logger.i("定位请求", reportReasonAttr);
                LocationService.cmdReqLocation(context, reportReasonAttr, priority);
            }
        } else if("com.wherecom.intent.action.PRAYER_LOCATION".equals(intent.getAction())){
            LocationService.prayerLocation(context);
        } else if("com.wherecom.intent.action.GET_DEVICE_LAST_LOCATION".equals(intent.getAction())){
            ApiService.getLastPosition();
        }
//        else if (CommonConstant.ACTION_UPLOAD_STEP.equals(intent.getAction())) {
//            //计步程序发送广播，请求上报计步数据
//            context.sendBroadcast(new Intent(context, AlarmReceiver.class).setAction(InternalConstant.ACTION_UPLOAD_STEP_ALARM));
//        }
        else if (Constants.ACTION_REFRESH_CLASSTIME.equals(intent.getAction())) {
            //上课禁用更新
            ApiService.getForbidPeriods(BaseApp.getInstance().deviceId);
        } else if (Constants.ACTION_REFRESH_CONCERNTIME.equals(intent.getAction())) {
            //特殊关爱时段更新
            ApiService.getConcernTime(BaseApp.getInstance().deviceId);
        } else if (Constants.ACTION_REFRESH_ALARMLIST.equals(intent.getAction())) {
            //闹钟列表更新
            ApiService.getAlarmList(BaseApp.getInstance().deviceId);
        }
//        else if (CommonConstant.ACTION_HANG_UP.equals(intent.getAction())) {
//            //手表通话结束定位
//            String name = intent.getStringExtra(CommonConstant.EXTRA_CONTACT_NAME);
//            int type = intent.getIntExtra(CommonConstant.EXTRA_CALL_TYPE, 0);
//            String phoneNumber = intent.getStringExtra(CommonConstant.EXTRA_PHONE_NUMBER);
//            long time = intent.getLongExtra(CommonConstant.EXTRA_OFFHOOK_TIME, 0);
//            int dur = intent.getIntExtra(CommonConstant.EXTRA_CALL_DURATION, 0);
//            String reportReasonAttr = phoneNumber + "#" + type + "#" + name + "#" + time + "#" + dur;
//            LogUtil.i("定位请求", reportReasonAttr);
//            if (dur > 0) {
//                //有通话时间上报位置
//                LocationService.requestLocationByCallEnd(context, reportReasonAttr);
//            }
//        } else if (CommonConstant.ACTION_GET_PETINFO.equals(intent.getAction())) {
//
//            InitService.getPetInfo(context);
//        } else if (CommonConstant.ACTION_REFRESH_BARRIER.equals(intent.getAction())) {
//            //刷新安全区域列表，先定位然后再重置定位闹钟
//            InitService.initBarrierList(context, InternalConstant.ACTION_REQUEST_LOCATION_ALARM);
//        } else if (CommonConstant.ACTION_INIT_SERVER_CONFIG.equals(intent.getAction())) {
//            //获取服务器配置信息
//            InitService.initServerConfig(context);
//        }
        else if (Constants.ACTION_INIT_DEVICE_CONFIG.equals(intent.getAction())) {
            //获取设备初始配置信息
            ApiService.getDeviceConfig();
        }
//        else if (CommonConstant.ACTION_REFRESH_WALLPAPERS.equals(intent.getAction())) {
//            //获取app设置的壁纸
//            InitService.downloadWallpaper(context, CommonConstant.ACTION_WALLPAPER_DOWNLOADED);
//        }
        else if (Constants.ACTION_REFRESH_WIFILIST.equals(intent.getAction())) {
            //获取app设置的wifi list
            ApiService.getWifiList(BaseApp.getInstance().deviceId);
        } else if (Constants.ACTION_UPLOAD_STORY_PLAY_HISTORY.equals(intent.getAction())) {
            ApiService.uploadStoryPlayHistory();
        }
    }
}
